﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class per_accout : MpageBase
{
    protected DataTable dt = new DataTable();
    protected Model.UsersAccount user_account = null;
    protected Model.CMember CMember = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        Bind();
        bindsearch();
    }
    private void Bind()
    {
        if (loginUserInfo != null && loginUserInfo.Id > 0)
        {
            user_account = BLL.UsersAccount.GetModelByuser_id(loginUserInfo.Id);
            CMember = BLL.CMember.GetModel(loginUserInfo.Id);
            if (user_account == null)
            {
                BLL.Session.Remove(BLL.CMember.LoginUserSessionKey);
                Response.Redirect("/default.aspx");
            }
        }
        else
        {
            Response.Redirect("/default.aspx");
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        bindsearch();
    }
    private void bindsearch()
    {
        //绑定下半部分查询列表
        StringBuilder sbrSQL = new StringBuilder();
        int typesl = int.Parse(typeslddl.SelectedValue);
        int times = int.Parse(timesl.SelectedValue);

        if (typesl != 1)
        {
            sbrSQL.Append("select cs.addtime as timer,(convert(varchar,'提现')+convert(varchar,cs.Id)) as typeid,(convert(varchar,'-')+convert(varchar,cs.total)) as sumoy, cs.fee as fee, ");
            sbrSQL.Append("(case cs.s_status when 0 then '待确认'  when 1 then '同意提现' when 2 then '驳回提现'  else '已删除' end)as state ");
            sbrSQL.Append(" from SF_user_acc_cash cs  where cs.user_id='" + loginUserInfo.Id + "'");
            if (times == 1)
            {
                sbrSQL.Append(" and datediff(day,cs.addtime ,Getdate()) <1");
            }
            else if (times == 2)
            {
                sbrSQL.Append(" and datediff(day,cs.addtime ,Getdate()) <=7");
            }
            else if (times == 3)
            {
                sbrSQL.Append(" and datediff(day,cs.addtime ,Getdate()) <=30");
            }
            else if (times == 4)
            {
                sbrSQL.Append(" and datediff(day,cs.addtime ,Getdate()) <=60");
            }
            else if (times == 5)
            {
                sbrSQL.Append(" and datediff(day,cs.addtime ,Getdate()) >90");
            }

        }
        if (typesl == 0)
        {
            sbrSQL.Append(" union ");
        }
        if (typesl != 2)
        {
            sbrSQL.Append("select od.ordertime as timer, (convert(varchar,'充值')+convert(varchar,od.Id)) as typeid,(convert(varchar,'+')+convert(varchar,od.total)) as sumoy, (od.total-od.realmon) as fee,");
            sbrSQL.Append("(case od.i_state when 0 then '待确认'  when 1 then '充值成功'  when 2 then '充值失败' else '已删除' end)as state");
            sbrSQL.Append(" from SF_u_rorder od  where  od.user_id='" + loginUserInfo.Id + "'");
            if (times == 1)
            {
                sbrSQL.Append(" and datediff(day,od.ordertime ,Getdate()) <1");
            }
            else if (times == 2)
            {
                sbrSQL.Append(" and datediff(day,od.ordertime ,Getdate()) <=7");
            }
            else if (times == 3)
            {
                sbrSQL.Append(" and datediff(day,od.ordertime ,Getdate()) <=30");
            }
            else if (times == 4)
            {
                sbrSQL.Append(" and datediff(day,od.ordertime ,Getdate()) <=60");
            }
            else if (times == 5)
            {
                sbrSQL.Append(" and datediff(day,od.ordertime ,Getdate()) >90");
            }

        }
        PagedDataSource pds = new PagedDataSource();
        DataTable dtold = new Data.DataAccess().GetDataTable<DataTable>(sbrSQL.ToString());
        dt = GetPagedTable(dtold, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize);
        if (dt != null && dt.Rows.Count > 0)
        {
            AspNetPager1.Visible = true;
            AspNetPager1.RecordCount = dtold.Rows.Count;
            pds.AllowPaging = true;
            pds.PageSize = AspNetPager1.PageSize;
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
        }
        else
        {
            AspNetPager1.Visible = false;
        }

    }
    /// <summary>
    /// DataTable分页
    /// </summary>
    /// <param name="dt">DataTable</param>
    /// <param name="PageIndex">页索引,注意：从1开始</param>
    /// <param name="PageSize">每页大小</param>
    /// <returns>分好页的DataTable数据</returns>              第1页        每页10条
    public static DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)
    {

        if (PageIndex == 0) { return dt; }
        DataTable newdt = dt.Copy();
        newdt.Clear();
        int rowbegin = (PageIndex - 1) * PageSize;
        int rowend = PageIndex * PageSize;

        if (rowbegin >= dt.Rows.Count)
        { return newdt; }

        if (rowend > dt.Rows.Count)
        { rowend = dt.Rows.Count; }
        for (int i = rowbegin; i <= rowend - 1; i++)
        {
            DataRow newdr = newdt.NewRow();
            DataRow dr = dt.Rows[i];
            foreach (DataColumn column in dt.Columns)
            {
                newdr[column.ColumnName] = dr[column.ColumnName];
            }
            newdt.Rows.Add(newdr);
        }
        return newdt;
    }

    /// <summary>
    /// 返回分页的页数
    /// </summary>
    /// <param name="count">总条数</param>
    /// <param name="pageye">每页显示多少条</param>
    /// <returns>如果 结尾为0：则返回1</returns>
    public static int PageCount(int count, int pageye)
    {
        int page = 0;
        int sesepage = pageye;
        if (count % sesepage == 0) { page = count / sesepage; }
        else { page = (count / sesepage) + 1; }
        if (page == 0) { page += 1; }
        return page;
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bindsearch();

    }
    protected void timesl_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind();
        bindsearch();
    }
    protected void typeslddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind();
        bindsearch();
    }
}